package com.apps.gomdiem.helper;

import java.util.ArrayList;

import org.w3c.dom.Document;

import android.os.AsyncTask;

import com.apps.gomdiem.map.api.GMapV2Direction;
import com.google.android.gms.maps.model.LatLng;

public class GetDirectionsAsyncTask extends
		AsyncTask<LatLng, Object, ArrayList<LatLng>> {

	public static final String USER_CURRENT_LAT = "user_current_lat";
	public static final String USER_CURRENT_LONG = "user_current_long";
	public static final String DESTINATION_LAT = "destination_lat";
	public static final String DESTINATION_LONG = "destination_long";
	public static final String DIRECTIONS_MODE = "directions_mode";
	private String url;

	private Exception exception;
	
	private OnGetDirectionListener onListener;

	public GetDirectionsAsyncTask(OnGetDirectionListener onListener /* String url */) {
		super();
		this.onListener = onListener;

		// this.url = url;
	}

	@Override
	public void onPostExecute(ArrayList<LatLng> result) {
		if (exception == null) {
			onListener.onGetDirectionComplete(result);
		} else {
			processException();
		}
	}

	@Override
	protected ArrayList<LatLng> doInBackground(LatLng... params) {

		try {
			GMapV2Direction md = new GMapV2Direction();
			Document doc = md.getDocument(params[0], params[1],
					GMapV2Direction.MODE_DRIVING);
			ArrayList<LatLng> directionPoints = md.getDirection(doc);
			return directionPoints;

		} catch (Exception e) {
			exception = e;
			return null;
		}
	}

	private void processException() {
	}

	public interface OnGetDirectionListener {
		public void onGetDirectionComplete(ArrayList<LatLng> result);
	}

}